package com.cskaoyan.service.system;

import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.MarketLog;
import com.cskaoyan.bean.po.MarketRole;
import com.cskaoyan.bean.po.MarketRoleExample;
import com.cskaoyan.bean.vo.RoleManagementOptionsVo;
import com.cskaoyan.bean.vo.systemVo.*;
import com.cskaoyan.mapper.MarketAdminMapper;
import com.cskaoyan.mapper.MarketRoleMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoleManagementServiceImpl implements RoleManagementService{


    @Autowired
    MarketRoleMapper marketRoleMapper;

    @Autowired
    MarketAdminMapper marketAdminMapper;

    @Override
    @Transactional
    public CommonData<RoleManagementOptionsVo> selectOptions() {
        MarketRoleExample marketRoleExample = new MarketRoleExample();
        List<MarketRole> marketRoles = marketRoleMapper.selectByExample(marketRoleExample);
        System.out.println(marketRoles);
        List<RoleManagementOptionsVo> roleManagementOptionsVos = new ArrayList<>();
        for (MarketRole marketRole : marketRoles) {
            RoleManagementOptionsVo roleManagementOptionsVo = new RoleManagementOptionsVo();
            roleManagementOptionsVo.setValue(marketRole.getId());
            roleManagementOptionsVo.setLabel(marketRole.getName());
            roleManagementOptionsVos.add(roleManagementOptionsVo);
        }
        CommonData<RoleManagementOptionsVo> roleManagementOptionsVoCommonData = new CommonData<>();
        roleManagementOptionsVoCommonData.setList(roleManagementOptionsVos);
        roleManagementOptionsVoCommonData.setTotal(roleManagementOptionsVos.size());
        roleManagementOptionsVoCommonData.setLimit(roleManagementOptionsVos.size());
        roleManagementOptionsVoCommonData.setPages(1);
        roleManagementOptionsVoCommonData.setPage(1);
        return roleManagementOptionsVoCommonData;
    }

    @Override
    @Transactional
    public CommonData<MarketRole> selectCommonData(Integer page, Integer limit, String sort, String order, String name) {
        PageHelper.startPage(page, limit);
        List<MarketRole> marketAdmins = marketRoleMapper.selectRole(name);
        CommonData<MarketRole> marketAdminCommonData = new CommonData<>();
        PageInfo<MarketRole> pageInfo = new PageInfo<>(marketAdmins);
        marketAdminCommonData.setLimit(limit);
        marketAdminCommonData.setPage(page);
        marketAdminCommonData.setTotal((int) pageInfo.getTotal());
        marketAdminCommonData.setPages(pageInfo.getPages());
        marketAdminCommonData.setList(pageInfo.getList());
        return marketAdminCommonData;
    }

    @Override
    @Transactional
    public MarketRole createRole(MarketRole marketRole) {
        marketRole.setAddTime(new Date());
        marketRole.setUpdateTime(new Date());
        marketRoleMapper.insertRole(marketRole);
        return marketRole;
    }

    @Override
    @Transactional
    public MarketRole updateRole(MarketRole marketRole) {
        marketRole.setUpdateTime(new Date());
        marketRoleMapper.updateByPrimaryKey(marketRole);
        return marketRole;
    }

    @Override
    @Transactional
    public Integer deleteRole(MarketRole marketRole) {
        int i = marketRoleMapper.deleteByPrimaryKey(marketRole.getId());
        return i;
    }

    @Override
    @Transactional
    public PermissionsVo selectPermissions(Integer roleId) {
        ArrayList<String> assignedPermissions = marketRoleMapper.selectPermissions(roleId);
        PermissionsVo permissionsVo = new PermissionsVo();
        System.out.println(assignedPermissions);
        if (assignedPermissions.size()!=0 && assignedPermissions.get(0).equals("*")){
            assignedPermissions = marketRoleMapper.selectTypePortAll();
        }
        permissionsVo.setAssignedPermissions(assignedPermissions);

        ArrayList<String> types = marketRoleMapper.selectType();
        ArrayList<SystemPermissions> systemPermissions = new ArrayList<>();
        for (String type : types) {
            SystemPermissions systemPermissions2 = new SystemPermissions();
            systemPermissions2.setLabel(type);
            systemPermissions2.setId(type);
            ArrayList<SystemPermissionsChildren> systemPermissionsChildren = new ArrayList<>();
            ArrayList<String> typesChildren =  marketRoleMapper.selectTypeChildren(type);
            for (String typesChild : typesChildren) {
                SystemPermissionsChildren systemPermissionsChildren1 = new SystemPermissionsChildren();
                systemPermissionsChildren1.setLabel(typesChild);
                systemPermissionsChildren1.setId(typesChild);
                ArrayList<TypePort> typePorts = marketRoleMapper.selectTypePort(typesChild);
                systemPermissionsChildren1.setChildren(typePorts);
                systemPermissionsChildren.add(systemPermissionsChildren1);
            }
            systemPermissions2.setChildren(systemPermissionsChildren);
            systemPermissions.add(systemPermissions2);
        }
        permissionsVo.setSystemPermissions(systemPermissions);
        System.out.println(permissionsVo);

        return permissionsVo;
    }

    @Override
    @Transactional
    public int permissionsPost(PermissionsBo permissionsBo) {
        Integer roleId = permissionsBo.getRoleId();
        if (roleId==1){
            return 641;
        }
       // ArrayList<String> permissions = marketRoleMapper.selectPermissions(roleId);

        marketRoleMapper.deletePermissionsByRoleId(roleId);
        Date date = new Date();
        ArrayList<String> permissions1 = permissionsBo.getPermissions();
        for (String permission : permissions1) {
            int i = marketRoleMapper.insertPermissions(roleId, permission);
            System.out.println(i);
        }



        return 0;
    }
}
